//
//  CatogryItemDetailViewController.m
//  project
//
//  Created by lanou on 15/10/28.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "CatogryItemDetailViewController.h"
#import "CatogryAllSpecialDetailCell.h"
#import "PKRequestManager.h"
#import "MJRefresh.h"
#import "StrategyDetailsViewController.h"
#import "OtherTableViewModel.h"

@interface CatogryItemDetailViewController () <UITableViewDataSource,UITableViewDelegate>

//创建一个tableView，是collectionItem之后的界面
@property(nonatomic,strong)UITableView *itemDetailTableView;
//创建一个接收数据的数组
@property(nonatomic,strong)NSMutableArray *dataSourceArray;

@end

@implementation CatogryItemDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //添加title
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake((kWidth - 80) / 2, 10, 80, 20)];
    titleLabel.text = _titleString;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    
    _itemDetailTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    _itemDetailTableView.backgroundColor = [UIColor whiteColor];
    _itemDetailTableView.dataSource = self;
    _itemDetailTableView.delegate = self;
    [_itemDetailTableView registerClass:[CatogryAllSpecialDetailCell class] forCellReuseIdentifier:@"CatogryAllSpecialDetailCell"];
    _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    _itemDetailTableView.rowHeight = 210;
    [self.view addSubview:_itemDetailTableView];
    
    [self requestData];
    
    //上拉加载
    __weak CatogryItemDetailViewController *newSelf = self;
    self.itemDetailTableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [newSelf requestPullData];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CatogryAllSpecialDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CatogryAllSpecialDetailCell" forIndexPath:indexPath];
    cell.model = _dataSourceArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    StrategyDetailsViewController *detailVC = [[StrategyDetailsViewController alloc]init];
    OtherTableViewModel *model = _dataSourceArray[indexPath.row];
    detailVC.contentId = model.Myid;
    detailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
}

-(void)requestData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    
    [PKRequestManager requestWithTypeP:GET urlString:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/%@/items?ad=1&limit=20&offset=0",_id] parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dict = dic[@"data"];
        NSArray *arr = dict[@"items"];
        for (NSDictionary *dicModel in arr)
        {
            OtherTableViewModel *model = [[OtherTableViewModel alloc]init];
            [model setValuesForKeysWithDictionary:dicModel];
            [_dataSourceArray addObject:model];
        }
        [_itemDetailTableView reloadData];
        
    } error:^(NSError *error) {
        NSLog(@"数据请求失败");
    }];
}

//上拉加载
static int offset = 0;
-(void)requestPullData
{
    offset += 20;
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    
    [PKRequestManager requestWithTypeP:GET urlString:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/%@/items?ad=1&limit=20&offset=%D",_id,offset] parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dict = dic[@"data"];
        NSArray *arr = dict[@"items"];
        for (NSDictionary *dicModel in arr)
        {
            OtherTableViewModel *model = [[OtherTableViewModel alloc]init];
            [model setValuesForKeysWithDictionary:dicModel];
            [_dataSourceArray addObject:model];
        }
        [_itemDetailTableView reloadData];
        [_itemDetailTableView.footer endRefreshing];
        
        
    } error:^(NSError *error) {
        NSLog(@"数据请求失败");
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
